package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallationOrderBean;
import com.cloudsigns.factorymanager2.engineering.customview.ReadDilalog;
import com.cloudsigns.factorymanager2.project.adapter.EngineeringInformationAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/1/14.
 */

public class ReceiveEngineeringBatchInstallationOrderAcitivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private ListView lv;
    private LinearLayout ll_bottom;
    private RelativeLayout rl_project;
    private ReadDilalog projectDialog, engineeringDialog;
    private LinearLayout ll_project_info_content;
    private ImageView img_project_select,img_infor_select;
    private TextView tv_receive_people_colon,tv_reception_time_colon;
    private TextView tv_project_name,edt_completion_date,edt_dispath_order_person_colon,edt_dispath_order_time_colon,edt_receive_people_colon,edt_reception_time_colon;
    private EngineeringInformationAdapter adapter;
    private List<EngineeringInstallationOrderBean> engineeringBeanList = new ArrayList<>();
    private final String TAG = "ReceiveEngineeringBatchInstallationOrderAcitivity";
    private TextView btn_receive_order, btn_back_order;
    private String type;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_receive_engineering_batch_installation_order);

        init();
    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        lv = findViewById(R.id.lv);
        rl_project = findViewById(R.id.rl_project);
        btn_receive_order = findViewById(R.id.btn_receive_order);
        btn_back_order = findViewById(R.id.btn_back_order);
        ll_project_info_content = findViewById(R.id.ll_project_info_content);
        img_project_select = findViewById(R.id.img_project_select);
        img_infor_select=findViewById(R.id.img_infor_select);
        ll_bottom=findViewById(R.id.ll_bottom);
        tv_project_name=findViewById(R.id.tv_project_name);
        edt_completion_date=findViewById(R.id.edt_completion_date);
        edt_dispath_order_person_colon=findViewById(R.id.edt_dispath_order_person_colon);
        edt_dispath_order_time_colon=findViewById(R.id.edt_dispath_order_time_colon);
        edt_receive_people_colon=findViewById(R.id.edt_receive_people_colon);
        edt_reception_time_colon=findViewById(R.id.edt_reception_time_colon);
        tv_receive_people_colon=findViewById(R.id.tv_receive_people_colon);
        tv_reception_time_colon=findViewById(R.id.tv_reception_time_colon);

        Intent intent=getIntent();
        type=intent.getStringExtra("type");
        if ("BackEngineeringOrderFragment".equals(type)){
            ll_bottom.setVisibility(View.GONE);
            tv_receive_people_colon.setText("退  回  人：");
            tv_reception_time_colon.setText("退回时间：");
        }

        if ("WattingToReceiveEngineeringOrderFragment".equals(type)){
            ll_bottom.setVisibility(View.VISIBLE);
        }

        tv_title.setText(getResources().getText(R.string.batch_installation));
        img_project_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        img_infor_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        rl_back.setOnClickListener(this);
        btn_receive_order.setOnClickListener(this);
        btn_back_order.setOnClickListener(this);
        img_project_select.setOnClickListener(this);
        img_infor_select.setOnClickListener(this);


        engineeringBeanList.add(new EngineeringInstallationOrderBean());
        engineeringBeanList.add(new EngineeringInstallationOrderBean());
        engineeringBeanList.add(new EngineeringInstallationOrderBean());


        adapter = new EngineeringInformationAdapter(this, TAG);
        adapter.setData(engineeringBeanList);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(this);
    }

    private void receiveOrder() {
        Toast.makeText(this, "接收成功", Toast.LENGTH_SHORT).show();
        btn_receive_order.setClickable(false);
        btn_receive_order.setBackgroundResource(R.mipmap.btn_bg_short_gray);
        btn_back_order.setClickable(false);
        btn_back_order.setBackgroundResource(R.mipmap.btn_bg_short_gray);
    }

    private void backOrder() {
        Toast.makeText(this, "退回成功", Toast.LENGTH_SHORT).show();
        btn_receive_order.setClickable(false);
        btn_receive_order.setBackgroundResource(R.mipmap.btn_bg_short_gray);
        btn_back_order.setClickable(false);
        btn_back_order.setBackgroundResource(R.mipmap.btn_bg_short_gray);
    }

    // 项目名称 收放
    private void showProjectInfoDialog() {
        if (ll_project_info_content.getVisibility() == View.VISIBLE) {
            ll_project_info_content.setVisibility(View.GONE);
            img_project_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        } else {
            ll_project_info_content.setVisibility(View.VISIBLE);
            img_project_select.setBackgroundResource(R.mipmap.arrow_top);
        }

//        projectDialog = new ReadDilalog(this);
//        projectDialog.createProjectInfoDialog();
//
//        projectDialog.show();
    }

    // 工程信息 收放
    private void showProjectInfoSelect() {
        if (lv.getVisibility() == View.VISIBLE) {
            lv.setVisibility(View.GONE);
            img_infor_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        } else {
            lv.setVisibility(View.VISIBLE);
            img_infor_select.setBackgroundResource(R.mipmap.arrow_top);
        }
    }

    private void showEngineeringInfoDialog(int positon) {
        EngineeringInstallationOrderBean bean = engineeringBeanList.get(positon);
        engineeringDialog = new ReadDilalog(this);
        engineeringDialog.createEngineeringInfoDialog("", null);

        engineeringDialog.show();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (projectDialog != null) {
            projectDialog.dismiss();
            projectDialog = null;
        }

        if (engineeringDialog != null) {
            engineeringDialog.dismiss();
            engineeringDialog = null;
        }


    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

        showEngineeringInfoDialog(i);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
                // 接收工单
            case R.id.btn_receive_order:
                receiveOrder();
                break;
                // 退回工单
            case R.id.btn_back_order:
                backOrder();
                break;
                // 项目名称 收放
            case R.id.img_project_select:
                showProjectInfoDialog();
                break;
            // 工程信息 收放
            case R.id.img_infor_select:
                showProjectInfoSelect();
                break;

        }
    }


}
